/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.college;

import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;

/**
 *
 * @author project
 */
public class CollegeRegister extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Connection con;
        Statement st;
        ResultSet rs;
        String error = "";
        boolean flag = true;
        String clgid, password, tponame, tpoemail, clgname, address, state, city, clgprofile, contact, fax, mobile, uname, url, status = "Not Verified", sql;
        HttpSession session = request.getSession();

        String dest = "", desterr = "";
        if (session.getAttribute("role") != null && session.getAttribute("role").equals("admin")) {
            dest = "admin/manage_cllg.jsp";
            desterr = "admin/addcollege.jsp";
        } else {
            dest="college.jsp";
            desterr = "college_registration.jsp";
        }
        PrintWriter out = response.getWriter();
        
        String captcha = (String) session.getAttribute("captcha");
        String code = (String) request.getParameter("code");
        //RequestDispatcher rd = request.getRequestDispatcher("college_registration.jsp");
        if (captcha != null && code != null) {

            if (!captcha.equals(code)) {
                request.setAttribute("captcha", "Not correct");
                response.sendRedirect(desterr+"?status=Invalid Captcha");
            }
        }
        try {
            con = Dbconn.getConn();
            st = con.createStatement();
            clgid = request.getParameter("clgid");
            password = request.getParameter("password");
            tponame = request.getParameter("tponame");
            tpoemail = request.getParameter("tpoemail");
            clgname = request.getParameter("cllgname");
            address = request.getParameter("address");
            state = request.getParameter("state");
            city = request.getParameter("city");
            clgprofile = request.getParameter("clgprofile");
            contact = request.getParameter("contact");
            fax = request.getParameter("fax");
            mobile = request.getParameter("mobile");
            uname = request.getParameter("uname");
            url = request.getParameter("url");
            out.println(clgid);
            rs = st.executeQuery("select * from college where clgid='" + clgid + "'");
            if (rs.next()) {
                response.sendRedirect("/college.jsp?status=Id already exist");
            } else {
                sql = "insert into college(clgid,password,tponame,tpoemail,clgname,address,state,city,clgprofile,contact,fax,mobile,uname,url,status) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setString(1, clgid);
                ps.setString(2, password);
                ps.setString(3, tponame);
                ps.setString(4, tpoemail);
                ps.setString(5, clgname);
                ps.setString(6, address);
                ps.setString(7, state);
                ps.setString(8, city);
                ps.setString(9, clgprofile);
                ps.setString(10, contact);
                ps.setString(11, fax);
                ps.setString(12, mobile);
                ps.setString(13, uname);
                ps.setString(14, url);
                ps.setString(15, status);
                ps.executeUpdate();
                request.setAttribute("status", "<font color='green' >College Registered successfully!! </font>");
                response.sendRedirect(dest+"?status=<font color='green' >College Registered successfully!! </font>");
            }
        } catch (Exception e) {
            request.setAttribute("status", "<font color='red' >Error occured! please try later </font>" + e);
            response.sendRedirect(dest+"?status=<font color='red' >Error occured! please try later </font>" + e);
            //rd.forward(request, response);
            //out.println(e);
        }
    }
}
